5931
8660
Estoy tratando de llamar a AsyncTask a través de un método de ParseQuerys Class para usar esa información en un ListView de mi Fragment lvRestaurantFrag. El problema es que el método devuelve nulo porque AsyncTask no llega a onPostExecute. No sé cómo puedo solucionarlo.
Aquí les dejo mi código:
JSON
Enlace API
[
{
"idBranchRestaurant": 1,
"idRestaurant": 1,
"restaurante":{
"idRestaurant": 1,
"nombre": "Don Julio",
"description": "La mejor parrilla para cualquier turista",
"red social":[
{
"idSocialNetworkRestaurant": 1,
"idRestaurant": 1,
"idTypeSocialNetwork": 1,
"valor": "www.donjulio.com.ar",
"escribe":{
"idTypeSocialNetwork": 1,
"nombre": "Sitio web",
"icon": "website_ic.jpg"
}
}
]
},
"idRangePriceBranch": 1,
"RangePrice": {
"idRangePriceBranch": 1,
"idBranchRestaurant": 1,
"mínimo": 300,
"máximo": 600
},
"nombre": "Paraguay",
"Foto":[
{
"idBranchPhoto": 4,
"idBranchRestaurant": 1,
"rama": nulo,
"idUser": 0,
"usuario": nulo,
"foto": "entradaDonJulio.jpg"
}
],
"cocina":[
{
"idCousine": 1,
"nombre": "Parrilla"
}
],
"menú":[
{
"idTypeMenu": 1,
"nombre": "Mediodía",
"idBranchRestaurant": 1,
"platos": nulo
},
{
"idTypeMenu": 2,
"nombre": "Noche",
"idBranchRestaurant": 1,
"platos": nulo
}
],
"filtrar":[
{
"idTypeFilter": 2,
"nombre": "Familia",
"icon": "family_ic.jpg"
}
],
"Servicio":[
{
"idService": 2,
"name": "Comida para celiacos"
},
{
"idService": 1,
"nombre": "Wifi"
}
],
"calendario":[
{
"idTimetableBranch": 11,
"idBranchRestaurant": 1,
"idDay": 2,
"openingHour": "19 hs",
"closedHour": "24 hs",
"día":{
"idDay": 2,
"name": "martes"
}
},
{
"idTimetableBranch": 10,
"idBranchRestaurant": 1,
"idDay": 2,
"openingHour": "12 hs",
"closedHour": "16 hs",
"día":{
"idDay": 2,
"name": "martes"
}
},
{
"idTimetableBranch": 9,
"idBranchRestaurant": 1,
"idDay": 1,
"openingHour": "19 hs",
"closedHour": "24 hs",
"día":{
"idDay": 1,
"name": "Lunes"
}
},
{
"idTimetableBranch": 8,
"idBranchRestaurant": 1,
"idDay": 1,
"openingHour": "12 hs",
"closedHour": "16 hs",
"día":{
"idDay": 1,
"name": "Lunes"
}
},
{
"idTimetableBranch": 12,
"idBranchRestaurant": 1,
"idDay": 3,
"openingHour": "12 hs",
"closedHour": "16 hs",
"día":{
"idDay": 3,
"name": "miércoles"
}
},
{
"idTimetableBranch": 13,
"idBranchRestaurant": 1,
"idDay": 3,
"openingHour": "19 hs",
"closedHour": "24 hs",
"día":{
"idDay": 3,
"name": "miércoles"
}
},
{
"idTimetableBranch": 14,
"idBranchRestaurant": 1,
"idDay": 4,
"openingHour": "12 hs",
"closedHour": "16 hs",
"día":{
"idDay": 4,
"name": "jueves"
}
},
{
"idTimetableBranch": 15,
"idBranchRestaurant": 1,
"idDay": 4,
"openingHour": "19 hs",
"closedHour": "24 hs",
"día":{
"idDay": 4,
"name": "jueves"
}
},
{
"idTimetableBranch": 16,
"idBranchRestaurant": 1,
"idDay": 5,
"openingHour": "12 hs",
"closedHour": "16 hs",
"día":{
"idDay": 5,
"name": "Viernes"
}
},
{
"idTimetableBranch": 17,
"idBranchRestaurant": 1,
"idDay": 5,
"openingHour": "19 hs",
"closedHour": "2 hs",
"día":{
"idDay": 5,
"name": "Viernes"
}
}
],
"latitud": "87654322",
"longitud": "23456789",
"calification": nulo,
"averageFood": 4.0,
"averageService": 4.0,
"averageAmbience": 4.0,
"averageCalification": 4.0,
"promoción":[
{
"idPromotion": 3,
"idTypePromotion": 2,
"código": "DIADELPADRE",
"name": "Dia Del Padre Regalo",
"startDate": "2017-06-26T00: 00: 00",
"expireDate": "2017-06-18T00: 00: 00",
"description": "postre de regalo",
"valor": 100,
"escribe":{
"idTypePromotion": 2,
"nombre": "Regalo"
}
}
]
},
{
"idBranchRestaurant": 2,
"idRestaurant": 2,
"restaurante":{
"idRestaurant": 2,
"nombre": "La Hormiga",
"description": "el lugar explota de gente y en especial de gente de ort",
"red social":[
{
"idSocialNetworkRestaurant": 2,
"idRestaurant": 2,
"idTypeSocialNetwork": 2,
"valor": "www.facebook.com/LaHormiga",
"escribe":{
"idTypeSocialNetwork": 2,
"nombre": "Facebook",
"icono": "fb_ic.jpg"
}
}
]
},
"idRangePriceBranch": 1,
"RangePrice": {
"idRangePriceBranch": 1,
"idBranchRestaurant": 1,
"mínimo": 300,
"máximo": 600
},
"nombre": "Ort",
"Foto":[
{
"idBranchPhoto": 5,
"idBranchRestaurant": 2,
"rama": nulo,
"idUser": 0,
"usuario": nulo,
"foto": "entradaLaHormiga.jpg"
}
],
"cocina":[
{
"idCousine": 2,
"name": "Pizza"
}
],
"menú":[
{
"idTypeMenu": 3,
"nombre": "Mediodía",
"idBranchRestaurant": 2,
"platos": nulo
}
],
"filtrar":[
{
"idTypeFilter": 1,
"name": "Con descuento",
"icon": "discount_ic.jpg"
}
],
"Servicio":[
{
"idService": 3,
"nombre": "Aire acondicionado"
},
{
"idService": 1,
"nombre": "Wifi"
}
],
"calendario":[
{
"idTimetableBranch": 18,
"idBranchRestaurant": 2,
"idDay": 6,
"openingHour": "11 hs",
"closedHour": "15 hs",
"día":{
"idDay": 6,
"name": "sábado"
}
},
{
"idTimetableBranch": 19,
"idBranchRestaurant": 2,
"idDay": 7,
"openingHour": "11 hs",
"closedHour": "15 hs",
"día":{
"idDay": 7,
"name": "Domingo"
}
}
],
"latitud": "234567897",
"longitud": "123456789",
"calification": nulo,
"comida media": 5,0,
"averageService": 5.0,
"averageAmbience": 5.0,
"averageCalification": 5.0,
"promoción":[
]
},
{
"idBranchRestaurant": 3,
"idRestaurant": 3,
"restaurante":{
"idRestaurant": 3,
"nombre": "paja rota",
"description": "La mejor parrilla de ramosmejia ",
"red social":[
{
"idSocialNetworkRestaurant": 3,
"idRestaurant": 3,
"idTypeSocialNetwork": 3,
"valor": "twitter.com/Pajarota",
"escribe":{
"idTypeSocialNetwork": 3,
"nombre": "Twitter",
"icono": "ic_twitter.png"
}
}
]
},
"idRangePriceBranch": 3,
"RangePrice": {
"idRangePriceBranch": 3,
"idBranchRestaurant": 3,
"mínimo": 50,
"máximo": 250
},
"nombre": "Ramos MEjia",
"Foto":[
{
"idBranchPhoto": 6,
"idBranchRestaurant": 3,
"rama": nulo,
"idUser": 0,
"usuario": nulo,
"foto": "EntradaPajaRota.jpg"
}
],
"cocina":[
{
"idCousine": 1,
"nombre": "Parrilla"
}
],
"menú":[
{
"idTypeMenu": 4,
"nombre": "Mediodía",
"idBranchRestaurant": 3,
"platos": nulo
},
{
"idTypeMenu": 5,
"nombre": "Noche",
"idBranchRestaurant": 3,
"platos": nulo
}
],
"filtrar":[
{
"idTypeFilter": 2,
"nombre": "Familia",
"icon": "family_ic.jpg"
}
],
"Servicio":[
{
"idService": 1,
"nombre": "Wifi"
},
{
"idService": 4,
"nombre": "Carta de braille"
}
],
"calendario":[
{
"idTimetableBranch": 20,
"idBranchRestaurant": 3,
"idDay": 5,
"openingHour": "12 hs",
"closedHour": "16 hs",
"día":{
"idDay": 5,
"name": "Viernes"
}
},
{
"idTimetableBranch": 21,
"idBranchRestaurant": 3,
"idDay": 5,
"openingHour": "19 hs",
"closedHour": "2 hs",
"día":{
"idDay": 5,
"name": "Viernes"
}
},
{
"idTimetableBranch": 22,
"idBranchRestaurant": 3,
"idDay": 6,
"openingHour": "12 hs",
"closedHour": "16 hs",
"día":{
"idDay": 6,
"name": "sábado"
}
},
{
"idTimetableBranch": 23,
"idBranchRestaurant": 3,
"idDay": 6,
"openingHour": "19 hs",
"closedHour": "2 hs",
"día":{
"idDay": 6,
"name": "sábado"
}
},
{
"idTimetableBranch": 24,
"idBranchRestaurant": 3,
"idDay": 7,
"openingHour": "12 hs",
"closedHour": "16 hs",
"día":{
"idDay": 7,
"name": "Domingo"
}
}
],
"latitud": "12345678",
"longitud": "87654321",
"calification": nulo,
"comida media": 5,0,
"averageService": 5.0,
"averageAmbience": 5.0,
"averageCalification": 5.0,
"promoción":[
]
},
{
"idBranchRestaurant": 4,
"idRestaurant": 4,
"restaurante":{
"idRestaurant": 4,
"nombre": "la espiga",
"description": "el mejor lugar para comer pastas",
"red social":[
{
"idSocialNetworkRestaurant": 4,
"idRestaurant": 4,
"idTypeSocialNetwork": 4,
"valor": "instagram.com/laespiga",
"escribe":{
"idTypeSocialNetwork": 4,
"nombre": "Instagram",
"icon": "ic_Instagram.png"
}
}
]
},
"idRangePriceBranch": 4,
"RangePrice": {
"idRangePriceBranch": 4,
"idBranchRestaurant": 4,
"mínimo": 80,
"máximo": 150
},
"nombre": "Almagro",
"Foto":[
{
"idBranchPhoto": 7,
"idBranchRestaurant": 4,
"rama": nulo,
"idUser": 0,
"usuario": nulo,
"foto": "EntradaLaEspiga.jpg"
}
],
"cocina":[
{
"idCousine": 4,
"name": "Pasta"
}
],
"menú":[
{
"idTypeMenu": 6,
"nombre": "Tarde",
"idBranchRestaurant": 4,
"platos": nulo
}
],
"filtrar":[
{
"idTypeFilter": 1,
"name": "Con descuento",
"icon": "discount_ic.jpg"
}
],
"Servicio":[
{
"idService": 1,
"nombre": "Wifi"
},
{
"idService": 3,
"nombre": "Aire acondicionado"
}
],
"calendario":[
{
"idTimetableBranch": 26,
"idBranchRestaurant": 4,
"idDay": 1,
"openingHour": "16 hs",
"closedHour": "19 hs",
"día":{
"idDay": 1,
"name": "Lunes"
}
},
{
"idTimetableBranch": 27,
"idBranchRestaurant": 4,
"idDay": 2,
"openingHour": "16 hs",
"closedHour": "19 hs",
"día":{
"idDay": 2,
"name": "martes"
}
},
{
"idTimetableBranch": 28,
"idBranchRestaurant": 4,
"idDay": 3,
"openingHour": "16 hs",
"closedHour": "19 hs",
"día":{
"idDay": 3,
"name": "miércoles"
}
},
{
"idTimetableBranch": 29,
"idBranchRestaurant": 4,
"idDay": 4,
"openingHour": "16 hs",
"closedHour": "19 hs",
"día":{
"idDay": 4,
"name": "jueves"
}
},
{
"idTimetableBranch": 30,
"idBranchRestaurant": 4,
"idDay": 5,
"openingHour": "16 hs",
"closedHour": "19 hs",
"día":{
"idDay": 5,
"name": "Viernes"
}
}
],
"latitud": "87657467",
"longitud": "45254567",
"calification": nulo,
"comida media": 5,0,
"averageService": 5.0,
"averageAmbience": 5.0,
"averageCalification": 5.0,
"promoción":[
]
}
]
Clase ParseQuery
servidor de cadenas privado = "http://appmorfando.azurewebsites.net/api/";
ParsingObjects parse = new ParsingObjects ();
// Consulta de ramas de lista
ramas de ArrayList  privadas = new ArrayList  ();
public ArrayList  getAllBranch (int limit, int offset) {
new BranchGetAll (). execute ("rama /", límite + "", desplazamiento + "");
devolver ramas;
}
la clase privada BranchGetAll extiende AsyncTask > {
protected void onPostExecute (ArrayList  datos) {
super.onPostExecute (datos);
sucursales = datos;
}
@Anular
protected ArrayList  doInBackground (String ... parametros) {
String url = parametros [0] + parametros [1] + "/" + parametros [2];
ArrayList  branchList = new ArrayList  ();
Cliente OkHttpClient = nuevo OkHttpClient ();
Solicitar solicitud = nuevo Request.Builder ()
.url (servidor + url)
.construir();
Resultado de cadena;
tratar {
Respuesta respuesta = client.newCall (request) .execute (); // Llamo al API Rest servicio1 en ejemplo.com
resultado = respuesta.cuerpo (). cadena ();
} captura (IOException e) {
Log.d ("Error", e.getMessage ()); // Error de red
devolver nulo;
}
tratar{
JSONArray jsonBranch = new JSONArray (resultado);
para (int i = 0; i  listBranches;
Public View onCreateView (Inflador LayoutInflater, grupo ViewGroup, datos del paquete) {
Ver toReturn;
toReturn = inflater.inflate (R.layout.frag_restaurant_lv, grupo, falso);
main = (MainActivity) getActivity ();
pq = nuevo ParseQuery ();
restaurantItems = (ListView) toReturn.findViewById (R.id.lvRestaurantItems);
// Aquí se pone nulo, por lo que el tamaño del adaptador es 0
listBranches = pq.getAllBranch (10,0);
adapter = new lvRestaurantAdapter (listBranches, principal);
restaurantItems.setAdapter (adaptador);
restaurantItems.setOnItemClickListener (esto);
volver a Volver;
}
@Anular
public void onItemClick (AdapterView  parent, View view, int position, long id) {
int idBranch = adapter.getId (posición);
main.BranchSelected (idBranch);
}
} 
2 cosas aquí:
¿Cómo sabe que su asynctask no llega a onpostexecute? Su código adjunto no anula onpostexecute. Intentemos implementar ese método y hagamos un logcat o system.out simple para confirmar.
Su asynctask obtiene objetos Json del servidor y los empaqueta en una lista para ser devueltos. Hay muchas formas de que esto falle, como una mala conexión al servidor o un análisis incorrecto de json. Probaría cuidadosamente su conexión y estructura de datos para asegurarme de que el nulo es algo que está causando el asynctask.
Una de las formas más sencillas de probar es comentar toda esa conexión y el código relacionado con json, luego devolver una lista rellenada manualmente que sabe que funciona. Esto ayudará a aislar el área que está fallando.
1
|
Tu respuesta
StackExchange.ifUsing ("editor", function () {
StackExchange.using ("editor externo", función () {
StackExchange.using ("fragmentos", función () {
StackExchange.snippets.init ();
});
});
}, "fragmentos de código");
StackExchange.ready (function () {
var channelOptions = {
etiquetas: "" .split (""),
id: "1"
};
initTagRenderer ("". split (""), "" .split (""), channelOptions);
StackExchange.using ("editor externo", función () {
// Debe activar el editor después de los fragmentos, si los fragmentos están habilitados
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using ("fragmentos", función () {
createEditor ();
});
}
demás {
createEditor ();
}
});
function createEditor () {
StackExchange.prepareEditor ({
useStacksEditor: false,
heartbeatType: 'respuesta',
autoActivateHeartbeat: falso,
convertImagesToLinks: verdadero,
noModals: cierto,
showLowRepImageUploadWarning: true,
reputacionToPostImages: 10,
bindNavPrevention: verdadero,
sufijo: "",
imageUploader: {
brandingHtml: "Desarrollado por \ u003ca href = \" https: //imgur.com/ \ "\ u003e \ u003csvg class = \" svg-icon \ "width = \" 50 \ "height = \" 18 \ "viewBox = \ "0 0 50 18 \" fill = \ "none \" xmlns = \ "http: //www.w3.org/2000/svg \" \ u003e \ u003cpath d = \ "M46.1709 9.17788C46.1709 8.26454 46.2665 7.94324 47.1084 7.58816C47.4091 7.46349 47.7169 7.36433 48.0099 7.26993C48.9099 6.97997 49.672 6.73443 49.672 5.93063C49.672 5.22043 48.9832 4.61182 48.1414 4.61182C47.4335 4.61182 46.725645.40531 43.1481 6.59048V11.9512C43.1481 13.2535 43.6264 13.8962 44.6595 13.8962C45.6924 13.8962 46.1709 13.2535 46.1709 11.9512V9.17788Z \ "/ \ u003e \ u003cpath d = \" M32.492 10.1419C32.492 14.648.942348 41.5985 12.6954 41.5985 10.1419V6.59049C41.5985 5.28821 41.1394 4.66232 40.1061 4.66232C39.0732 4.66232 38.5948 5.28821 38.5948 6.59049V9.60062C38.5948 10.8521 38.2696 11.5455 37.0451 11.5454.5C35.8 3520.49455C35.8 521 35.4954 9.60062V6.59049C35.4954 5.28821 35.0173 4.66232 34.0034 4.66232C32.9703 4.66232 32.492 5.28821 32.492 6.59049V10.1419Z \ "/ \ u003e \ u003cpath fill-rule = \" evenodd \ "clip-rule = \" evenodd \ "d = \ "M25.6622 17.6335C27.8049 17.6335 29.3739 16.9402 30.2537 15.6379C30.8468 14.7755 30.9615 13.5579 30.9615 11.9512V6.59049C30.9615 5.28821 30.4833 4.66231 29.4502 4.66231C28.9913 4.662025C 289.15527 .1369 4.56087 21.0134 6.57349 21.0134 9.27932C21.0134 11.9852 23.003 13.913 25.3754 13.913C26.5612 13.913 27.4607 13.4902 28.1109 12.6616C28.1109 12.7229 28.1161 12.7799 28.121 12.8346C28.92.125.283 28.121 12.8346C2834.232.283 28.121 12.8346C2834.1256.28 15.2321 24.1352 14.9821 23.5661 14.7787C23.176 14.6393 22.8472 14.5218 22.5437 14.5218C21.7977 14.5218 21.2429 15.0123 21.2429 15.6887C21.2429 16.7375 22.9072 17.6335 25.6622 17.6317.10.99 732.943CM 27.2119 7.09766 28.0918 7.94324 28.0918 9.27932C28.0918 10.6321 27.2311 11.5116 26.1024 11.5116C24.9737 11.5116 24.1317 10.6491 24.1317 9.27932Z \ "/ \ u003e \ u003cpath d = \" M16.802802.86.9512C358. 19.8079 13.2535 19.8079 11.9512V8.12928C19.8079 5.82936 18.4879 4.62866 16.4027 4.62866C15.1594 4.62866 14.279 4.98375 13.3609 5.88013C12.653 5.05154 11.6581 4.62866 10.3573 4.62866C9.34336 4.62866 4,62866 780793 4,8993 5.00066 5.28821 5.00066 6.59049V11.9512C5.00066 13.2535 5.47873 13.8962 6.51203 13.8962C7.54479 13.8962 8.0232 13.2535 8.0232 11.9512V8.90741C8.0232 7.58817 8.44431 6.91179 9.53458 6.91179C10.593V 9.91179 9.53458 6.91179C10.51093 6.9411811 C13.4375 13.8962 13.9157 13.2535 13.9157 11.9512V8.90741C13.9157 7.58817 14.3365 6.91179 15.4269 6.91179C16.4027 6.91179 16.8045 7.58817 16.8045 8.94108V11.9512Z \ "/ \ u003e \ u003cpath d = \ "M3.31675 6.59049C3.31675 5.28821 2.83866 4.66232 1.82471 4.66232C0.791758 4.66232 0.313354 5.28821 0.313354 6.59049V11.9512C0.313354 13.2535 0.791758 13.8962 1.82471 13.8962C2.8571675 13.8962 3.31335411.9512V6.59049Z \ "/ \ u003e \ u003cpath d = \" M1.87209 0.400291C0.843612 0.400291 0 1.1159 0 1.98861C0 2.87869 0.822846 3.57676 1.87209 3.57676C2.90056 3.57676 3.7234 2.87869 3.7234 1.0006113 \ "fill = \" # 1BB76E \ "/ \ u003e \ u003c / svg \ u003e \ u003c / a \ u003e",
contentPolicyHtml: "Contribuciones de usuario con licencia bajo \ u003ca href = \" https: //stackoverflow.com/help/licensing \ "\ u003ecc by-sa \ u003c / a \ u003e \ u003ca href = \" https://stackoverflow.com / legal / content-policy \ "\ u003e (política de contenido) \ u003c / a \ u003e",
allowUrls: verdadero
},
onDemand: verdadero,
discardSelector: ".discard-answer"
, inmediatamenteShowMarkdownHelp: true, enableTables: true, enableSnippets: true
});
}
});
¡Gracias por contribuir con una respuesta a Stack Overflow!
Asegúrese de responder la pregunta. ¡Proporcione detalles y comparta su investigación!
Pero evita ...
Pedir ayuda, aclaraciones o responder a otras respuestas.
Hacer declaraciones basadas en opiniones; respóndelos con referencias o experiencia personal.
Para obtener más información, consulte nuestros consejos sobre cómo escribir buenas respuestas.
Borrador guardado
Borrador descartado
Regístrate o inicia sesión
StackExchange.ready (function () {
StackExchange.helpers.onClickDraftSave ('# login-link');
});
Regístrese con Google
Registrarse usando Facebook
Regístrese con correo electrónico y contraseña
Enviar
Publicar como invitado
Nombre
Correo electrónico
Requerido, pero nunca mostrado
StackExchange.ready (
function () {
StackExchange.openid.initPostLogin ('. New-post-login', 'https% 3a% 2f% 2fstackoverflow.com% 2fquestions% 2f45538107% 2fcall-asynctack-from-other-class-and-return-null-android-studio% 23new-answer ',' question_page ');
}
);
Publicar como invitado
Nombre
Correo electrónico
Requerido, pero nunca mostrado
Publica tu respuesta
Descarte
Al hacer clic en "Publicar su respuesta", acepta nuestros términos de servicio, política de privacidad y política de cookies.
No es la respuesta que estás buscando? Lea otras preguntas con la etiqueta java android json android-asynctask o formule su propia pregunta.